package com.haitaoit.chengquan.module.home.activity.pinpai;

import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BaseActivity;
import com.haitaoit.chengquan.utils.Constant;
import com.haitaoit.chengquan.utils.GetSign;
import com.haitaoit.chengquan.utils.HttpUtilsSingle;
import com.haitaoit.chengquan.utils.NameValuePairSign;
import com.haitaoit.chengquan.utils.ToastUtils;
import com.haitaoit.chengquan.view.MyWebView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class BrandDetailActivity extends BaseActivity {
    @ViewInject(R.id.tv_title)
    TextView tv_title;
    @ViewInject(R.id.tv_addtime)
    TextView tv_addtime;
    @ViewInject(R.id.webview)
    MyWebView webview;


    private String id;
    private String titleName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_brand_detail);
        ViewUtils.inject(this);


        setTitle_V("埃森特机床");
        setLeftShow(1, R.mipmap.left);
//        setRightShow(1, R.mipmap.img56);
        backLeft_V();
        id = getIntent().getStringExtra("id");
        titleName = getIntent().getStringExtra("titlename");
        if (titleName!=null){
            setTitle_V(titleName);
        }



        //给WebVIew设置进度条
        WebSettings seting=webview.getSettings();
        seting.setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient(){

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);

                if (newProgress==100){
                    hideProgree();
                }else {

                    showProgrees("");
                }
            }

        });







        httpBrandDetial();
    }

    private void httpBrandDetial() {
        List<NameValuePairSign> pairSigns = new ArrayList<>();
        pairSigns.add(new NameValuePairSign("id", id));
        String sign = GetSign.genPackageSign(pairSigns);

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("id", id);
        params.addQueryStringParameter("sign", sign);
        HttpUtilsSingle.doGet(this, false, Constant.Getbrand_recommendation_model, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result.toString();
                try {
                    JSONObject json = new JSONObject(result);
                    String ErrCode = json.getString("ErrCode");
                    String ErrMsg = json.getString("ErrCode");
                    JSONObject obj = json.getJSONObject("Response");
                    if (Integer.valueOf(ErrCode) == 0) {
                        String name = obj.getString("name");
                        String content = obj.getString("content");
                        String addtime = obj.getString("addtime");
                        tv_title.setText(name);
                        tv_addtime.setText(addtime);
                        webview.loadDataWithBaseURL("about:blank", content, "text/html", "utf-8", null);
                    } else {
                        ToastUtils.showToast(ErrMsg);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                ToastUtils.showToast("获取数据失败，请检查网路！");
            }
        });

    }
}
